package com.apec.delay.producer;

import com.apec.delay.constants.DelayConstants;
import com.apec.delay.constants.DelayTypeEnum;
import com.apec.delay.consumer.DeadLetterQueueConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class DelayMessageSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String msg, DelayTypeEnum type) {
        log.info("当前时间：{},发送的消息：{},消息类型{}", DeadLetterQueueConsumer.formatDate(new Date()), msg, type);
        switch (type) {
            case DELAY_10s:
                rabbitTemplate.convertAndSend(DelayConstants.DELAY_EXCHANGE_NAME, DelayConstants.DELAY_QUEUEA_ROUTING_KEY, msg);
                break;
            case DELAY_60s:
                rabbitTemplate.convertAndSend(DelayConstants.DELAY_EXCHANGE_NAME, DelayConstants.DELAY_QUEUEB_ROUTING_KEY, msg);
                break;
        }
    }

    public void sendDelayMsg(String msg, Integer delayTime) {
        rabbitTemplate.convertAndSend(DelayConstants.DELAYED_EXCHANGE_NAME, DelayConstants.DELAYED_ROUTING_KEY, msg, a ->{
            a.getMessageProperties().setDelay(delayTime);
            return a;
        });
    }
}